package com.sth.swing;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author: sitianhong
 * @Description:
 * @Since: create in 2021/11/25 0025 9:54
 */
public class MyFJFileChooserTest2 {
    private JLabel label = new JLabel("所选文件路径：");
    private JTextField jtf = new JTextField(25);
    private JButton button = new JButton("浏览");

    public MyFJFileChooserTest2() {
        JFrame jf = new JFrame("文件选择器");
        JPanel panel = new JPanel();
        panel.add(label);
        panel.add(jtf);
        panel.add(button);
        jf.add(panel);
        jf.pack();    //自动调整大小
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(new MyActionListener());    //监听按钮事件
    }

    //Action事件处理
    class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            JFileChooser fc = new JFileChooser("F:\\");
            int val = fc.showOpenDialog(null);    //文件打开对话框
           // fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        }
    }
}
